package com.crm.service.system.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.constant.NoticeTypeConstant;
import com.crm.model.activity.pojos.Activity;
import com.crm.model.common.dtos.PageResponseResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.model.system.notic.dtos.NoticeDto;
import com.crm.model.system.notic.dtos.NoticePageDto;
import com.crm.model.system.notic.pojos.Notice;
import com.crm.service.crm.transfer.mapper.RedeployMapper;
import com.crm.service.system.notice.mapper.NoticeMapper;
import com.crm.service.system.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper,Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private RedeployMapper redeployMapper;
/*
    @Autowired
    private ActivityMapper activityMapper;
*/
/*    @Autowired
    private ContractMapper contractMapper;*/


    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findPage(NoticePageDto dto) {
        //校验参数
        dto.checkParam();
        if (dto.getTag() != NoticeTypeConstant.READED && dto.getTag() != NoticeTypeConstant.UNREAD){
            dto.setTag(NoticeTypeConstant.UNREAD);
        }

        IPage<Notice> page = new Page<>(dto.getPage(),dto.getSize());
        if (dto.getTag().equals(NoticeTypeConstant.UNREAD)){
            noticeMapper.selectPage(page,
                    new LambdaQueryWrapper<Notice>()
                            .eq(Notice::getTag,NoticeTypeConstant.UNREAD));
        }else {
            noticeMapper.selectPage(page,
                    new LambdaQueryWrapper<Notice>()
                            .eq(Notice::getTag,NoticeTypeConstant.READED));
        }

        //封装数据
        PageResponseResult result = new PageResponseResult();
        result.setCurrentPage((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setTotal((int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增通知
     * @param noticeDto
     * @return
     */
    @Override
    public ResponseResult<String> addNotice(NoticeDto noticeDto) {
        //校验数据
        if (noticeDto.getType() == null || noticeDto.getUserId() == null
                || noticeDto.getUserName() == null || noticeDto.getStatus() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断类型

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
